package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeConUserCompeteMapper;
import com.htgd.gardenexpo.dao.WxUserAchievementMapper;
import com.htgd.gardenexpo.entity.GeConAchievement;
import com.htgd.gardenexpo.dao.GeConAchievementMapper;
import com.htgd.gardenexpo.entity.WxUser;
import com.htgd.gardenexpo.entity.WxUserAchievement;
import com.htgd.gardenexpo.page.AchievementQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeConAchievementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.GeConAchievementVO;
import com.htgd.gardenexpo.vo.MyAchievementItemVO;
import com.htgd.gardenexpo.vo.MyAchievementVO;
import com.htgd.gardenexpo.vo.WxUserAchievementVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 成就管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-20
 */
@Service
public class GeConAchievementServiceImpl extends ServiceImpl<GeConAchievementMapper, GeConAchievement> implements GeConAchievementService {

    @Autowired
    private GeConAchievementMapper geConAchievementMapper;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Autowired
    private GeConUserCompeteMapper geConUserCompeteMapper;

    @Autowired
    private WxUserAchievementMapper wxUserAchievementMapper;

    @Override
    public PageInfo<GeConAchievementVO> pageList(AchievementQuery achievementQuery) {
        PageHelper.startPage(achievementQuery.getPageNum(), achievementQuery.getPageSize());
        List<GeConAchievementVO> list= geConAchievementMapper.pageList(achievementQuery);
        for (GeConAchievementVO geConAchievementVO : list) {
            if(geConAchievementVO.getStatus()!=null){
                geConAchievementVO.setStatusText(geBaseDictService.getStatusText(String.valueOf(geConAchievementVO.getStatus())));
            }

            if(geConAchievementVO.getIsTop()!=null){
                geConAchievementVO.setIsTopText(geBaseDictService.getTopText(String.valueOf(geConAchievementVO.getIsTop())));
            }

            if(geConAchievementVO.getCategory()!=null){//类别
                geConAchievementVO.setCategoryText(geBaseDictService.getTextByCodeAndValue("category_achievement",geConAchievementVO.getCategory()));
            }

            if(geConAchievementVO.getType()!=null){//类型
                geConAchievementVO.setTypeText(geBaseDictService.getTextByCodeAndValue("type_achievement",geConAchievementVO.getType()));
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public MyAchievementVO getMyAchievement(WxUser wxUser) {
        MyAchievementVO myAchievementVOS = new MyAchievementVO();

        // 计算过去日期到当前日期的天数差
        long daysBetween = ChronoUnit.DAYS.between(wxUser.getCreateTime(), LocalDateTime.now());
        myAchievementVOS.setName(wxUser.getName());
        myAchievementVOS.setDay(daysBetween);

        //运动成就
        List<MyAchievementItemVO> sportAchievement = new ArrayList<>();

        //我获得的运动成就
        List<MyAchievementItemVO> wxUserAchievementVOList = wxUserAchievementMapper.getByOpenIdAndCategory(wxUser.getOpenId(),"1");
        List<String> ids= new ArrayList<>();
        for (MyAchievementItemVO myAchievementItemVO : wxUserAchievementVOList) {
            myAchievementItemVO.setFlag(true);
            ids.add(myAchievementItemVO.getId());
        }
        sportAchievement.addAll(wxUserAchievementVOList);

        //未获得的运动成就
        QueryWrapper<GeConAchievement> geConAchievementQueryWrapper = new QueryWrapper<>();
        geConAchievementQueryWrapper.eq("status", "3")
                .eq("category","1")
                .eq("del_flag",0);
        if(ids.size()>0){
            geConAchievementQueryWrapper.notIn("id",ids);
        }
        List<GeConAchievement> sportList = geConAchievementMapper.selectList(geConAchievementQueryWrapper);
        for (GeConAchievement geConAchievement : sportList) {
            MyAchievementItemVO myAchievementItemVO = new MyAchievementItemVO();
            BeanUtils.copyProperties(geConAchievement,myAchievementItemVO);
            myAchievementItemVO.setFlag(false);
            sportAchievement.add(myAchievementItemVO);
        }
        sportAchievement.sort((o1, o2) -> Boolean.compare(o2.isFlag(), o1.isFlag()));
        myAchievementVOS.setSportAchievement(sportAchievement);


        //赛事成就
        List<MyAchievementItemVO> competitionAchievement = new ArrayList<>();

        //我获得的赛事成就
        List<MyAchievementItemVO> wxUserAchievementVOList2 = wxUserAchievementMapper.getByOpenIdAndCategory(wxUser.getOpenId(),"2");
        List<String> ids2= new ArrayList<>();
        for (MyAchievementItemVO myAchievementItemVO : wxUserAchievementVOList2) {
            myAchievementItemVO.setFlag(true);
            ids2.add(myAchievementItemVO.getId());
        }
        competitionAchievement.addAll(wxUserAchievementVOList2);

        //未获得的赛事成就
        QueryWrapper<GeConAchievement> geConAchievementQueryWrapper2 = new QueryWrapper<>();
        geConAchievementQueryWrapper2.eq("status", "3")
                .eq("category","2")
                .eq("del_flag",0);
        if(ids2.size()>0){
            geConAchievementQueryWrapper2.notIn("id",ids2);
        }
        List<GeConAchievement> competitionList = geConAchievementMapper.selectList(geConAchievementQueryWrapper2);
        for (GeConAchievement geConAchievement : competitionList) {
            MyAchievementItemVO myAchievementItemVO = new MyAchievementItemVO();
            BeanUtils.copyProperties(geConAchievement,myAchievementItemVO);
            myAchievementItemVO.setFlag(false);
            competitionAchievement.add(myAchievementItemVO);
        }
        competitionAchievement.sort((o1, o2) -> Boolean.compare(o2.isFlag(), o1.isFlag()));
        myAchievementVOS.setCompetitionAchievement(competitionAchievement);
        return myAchievementVOS;
    }

    @Override
    public int updatePersonalAchievement(WxUser wxUser) {
        int num = 0;
        //运动成就
        List<GeConAchievement> sportList = lambdaQuery().eq(GeConAchievement::getStatus, "3")
                .eq(GeConAchievement::getCategory,"1")
                .eq(GeConAchievement::getDelFlag,0)
                .list();
        for (GeConAchievement geConAchievement : sportList) {
            if("1".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsDays = geConUserCompeteMapper.getCountSportsDays("1",wxUser.getOpenId());
                if(sportsDays>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
            //时长
            if("2".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsTimes = geConUserCompeteMapper.getCountSportsTimes("1",wxUser.getOpenId());
                if(sportsTimes>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
            //里程
            if("3".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsMileages = geConUserCompeteMapper.getCountSportsMileages("1",wxUser.getOpenId());
                if(sportsMileages>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
        }

        //赛事成就
        List<GeConAchievement> competitionList = lambdaQuery().eq(GeConAchievement::getStatus, "3")
                .eq(GeConAchievement::getCategory,"2")
                .list();
        for (GeConAchievement geConAchievement : competitionList) {
            //天数
            if("1".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsDays = geConUserCompeteMapper.getCountSportsDays("2",wxUser.getOpenId());
                if(sportsDays>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
            //时长
            if("2".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsTimes = geConUserCompeteMapper.getCountSportsTimes("2",wxUser.getOpenId());
                if(sportsTimes>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
            //里程
            if("3".equals(geConAchievement.getType())){
                int condition = geConAchievement.getCondition();
                int sportsMileages = geConUserCompeteMapper.getCountSportsMileages("2",wxUser.getOpenId());
                if(sportsMileages>=condition){//满足
                    //插入成就记录
                    WxUserAchievement getByOpenIdAndAchievementId = wxUserAchievementMapper.getByOpenIdAndAchievementId(wxUser.getOpenId(),geConAchievement.getId());
                    if(getByOpenIdAndAchievementId==null){
                        WxUserAchievement wxUserAchievement = new WxUserAchievement();
                        wxUserAchievement.setOpenId(wxUser.getOpenId());
                        wxUserAchievement.setAchievementId(geConAchievement.getId());
                        wxUserAchievementMapper.insert(wxUserAchievement);
                        num++;
                    }
                }
            }
        }
        return num;
    }

    @Override
    public int getTotalNum(String openId) {
        //我的成就
        QueryWrapper<WxUserAchievement> wxUserAchievementQueryWrapper = new QueryWrapper<>();
        wxUserAchievementQueryWrapper.eq("open_id",openId);
        List<WxUserAchievement> wxUserAchievements = wxUserAchievementMapper.selectList(wxUserAchievementQueryWrapper);
        List<String> ids = new ArrayList<>();
        for (WxUserAchievement wxUserAchievement : wxUserAchievements) {
            ids.add(wxUserAchievement.getAchievementId());
        }

        // 构建查询条件
        QueryWrapper<GeConAchievement> geConAchievementQueryWrapper = new QueryWrapper<>();
        geConAchievementQueryWrapper.eq("status", "3");

        // 如果 ids 非空，添加 .notIn() 条件
        if (!ids.isEmpty()) {
            geConAchievementQueryWrapper.notIn("id", ids);
        }

        // 获取符合条件的记录数
        Long count = geConAchievementMapper.selectCount(geConAchievementQueryWrapper);
        return Math.toIntExact(wxUserAchievements.size() + count);
    }
}
